package com.qf.hello.spring.life;

import com.qf.hello.spring.service.impl.UserServiceImpl;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * 自定义的后置处理器
 * 可以在bean的初始化前后做一些其他工作
 */
public class MypostProcess implements BeanPostProcessor {

    //所有的bean在初始化之前都可以执行当前的代码
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("初始化之前的生命周期");
        return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
    }


    //所有的bean在初始化之后都可以执行当前的代码
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof UserServiceImpl){
            System.out.println("初始化之后的生命周期");
        }
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }
}
